package com.ljy.my_study.java.gc.reference;

import java.lang.ref.SoftReference;

/**
 * @author lijunying
 * @date 2021/1/6 14:07
 */
public class SoftRef {

    public static class User{
        public User(int id,String name){
            this.id=id;
            this.name=name;
        }

        public int id;
        public String name;

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    /**
     * -Xmx10m
     * @param args
     */
    public static void main(String[] args) {
        User u=new User(1,"James");
        SoftReference<User> softReference=new SoftReference<>(u);
        u=null;
        System.out.println(softReference.get());
        System.gc();
        System.out.println("after gc");
        System.out.println(softReference.get());
        byte[] b=new byte[1024*925*7];
        //由于资源紧张，软引用被清除
        System.out.println(softReference.get());
    }
}
